/* 
封装ajax请求的自定义hook函数
*/
import axios from 'axios'

import {ref} from 'vue'
export default function useUrlLoader<T>(url: string) {
  const result = ref<T>()
  const loading = ref<boolean>(true)
  const errorMsg = ref<string>('')

  axios(url)
    .then(response => {
      loading.value = false
      result.value = response.data
    })
    .catch(error => {
      loading.value = false
      errorMsg.value = error.message
    })

  return {
    result,
    loading,
    errorMsg
  }
}